<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.state ? $t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" top="20vh" width="490px" @close='close'>
    <div class="dialogContentBoxMaxHeight60">
      <el-form ref="form" :model="record" label-width="120px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('IPv4地址')" prop="ip" :key='key'>
          <el-input v-emoji v-model="record.ip" ref="recordIp" v-if="!record.state" style="width:270px;" size='small'></el-input>
          <span v-else>{{ record.ip }}</span>
        </el-form-item>
        <el-form-item :label="$t('MAC地址')" prop="mac">
          <el-input v-emoji v-model="record.mac" ref="mac" style="width:270px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('网口')" prop="dev">
          <el-select v-if="!record.state" v-model="record.dev" ref="dev" :placeholder="$t('请选择出接口')" clearable filterable size='small' style="width:270px;">
            <el-option v-for="item in policyList" :disabled="item.enPortMode === 3" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
          <span v-else>{{ record.dev }}</span>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addArp, editArp } from '@/api/network/mac/arp'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'ArpDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateIpv4 = (rule, value, callback) => {
      let regexp = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!regexp.test(value)) {
        callback(new Error(this.$t('请输入正确的ip地址')))
      } else {
        callback()
      }
    }
    var validateMac = (rule, value, callback) => {
      let regexp = /[A-Fa-f0-9]{2}[:-][A-Fa-f0-9]{2}[:-][A-Fa-f0-9]{2}[:-][A-Fa-f0-9]{2}[:-][A-Fa-f0-9]{2}[:-][A-Fa-f0-9]{2}/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!regexp.test(value)) {
        callback(new Error(this.$t('请输入正确的mac地址')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: null,
        ip: '',
        mac: '',
        dev: '',
        is_static: '',
        state: ''
      },
      recordCopy: {
        id: null,
        ip: '',
        mac: '',
        dev: '',
        is_static: '',
        state: ''
      },
      policyList: [],
      rules: {
        ip: [{ required: false, validator: validateIpv4, trigger: 'blur' }],
        mac: [{ required: false, validator: validateMac, trigger: 'blur' }],
        dev: [{ required: false, validator: validateName, trigger: 'change' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data1 = ['mac']
        inputClass(this.$refs, data1)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
      } else if (data === undefined) {
        this.$nextTick(() => {
          const data2 = ['recordIp']
          const selectData = ['dev']
          inputClass(this.$refs, data2)
          selectClass(this.$refs, selectData)
        })
      }
      getPhysicalApiArray().then(res => {
        if (res.status === 0) {
          this.policyList = res.info
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.record.state) {
            addArp(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            editArp(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.policyList = []
      this.$refs.form.resetFields()
    }
  }
}
</script>
